package com.power.using.myapplication.utils;

import android.text.TextUtils;
import android.util.Log;

import com.google.gson.Gson;
import com.google.gson.JsonArray;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import com.google.gson.reflect.TypeToken;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * GoogleJson������
 *
 * @author Administrator
 */
public class GsonTools {

    public GsonTools() {
    }

    public static String createGsonString(Object object) {
        Gson gson = new Gson();
        String gsonString = gson.toJson(object);
        return gsonString;
    }

    public static <T> T changeGsonToBean(String gsonString, Class<T> cls) {
        T t = null;
        try {
            Gson gson = new Gson();
            t = gson.fromJson(gsonString, cls);
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
            Log.e("Gson", "数据解析有误"+e.toString());
        }
        return t;
    }

    public static <T> List<T> changeGsonToList(String gsonString, Class<T> cls) {
        List<T> list = null;
        try {
            Gson gson = new Gson();
            list = gson.fromJson(gsonString, new TypeToken<List<T>>() {
            }.getType());
        } catch (JsonSyntaxException e) {
            e.printStackTrace();
            Log.e("Gson", "数据解析有误");
        }
        return list;
    }

    public static <T> List<Map<String, T>> changeGsonToListMaps(String gsonString) {
        List<Map<String, T>> list = null;
        try {
            Gson gson = new Gson();
            list = gson.fromJson(gsonString, new TypeToken<List<Map<String, T>>>() {
            }.getType());
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Gson", "数据解析有误");
        }
        return list;
    }

    public static <T> Map<String, T> changeGsonToMaps(String gsonString) {
        Map<String, T> map = null;
        try {
            Gson gson = new Gson();
            map = gson.fromJson(gsonString, new TypeToken<Map<String, T>>() {
            }.getType());
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Gson", "数据解析有误");
        }
        return map;
    }

    public static <T> List<T> changeGsonObjectList(String jsonString, Class<T> cls) {
        List<T> list = new ArrayList<T>();
        try {
            Gson gson = new Gson();
            JsonArray arry = new JsonParser().parse(jsonString).getAsJsonArray();
            for (JsonElement jsonElement : arry) {
                list.add(gson.fromJson(jsonElement, cls));
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Gson", "数据解析有误");

        }
        return list;
    }


    //JsonObject 单个解析 仅限
    public static Object changeJsonObject(String jsonStr, String content) {
        if (TextUtils.isEmpty(jsonStr)) return jsonStr;
        try {
            JSONObject object = new JSONObject(jsonStr);
            return object.get(content);

        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    public static void changeJsonObjects(String jsonStr, String... content){
        if (TextUtils.isEmpty(jsonStr)) return ;


    }




}
